package com.company.goodfuture2021;

public class Main2 {
    public ListNode reverseList (ListNode head) {
        // write code here
        if(head == null || head.next == null){
            return head;
        }
        ListNode dummy = new ListNode(-1);
        while (head != null){
            ListNode nextNode = head.next;
            head.next = dummy.next;
            dummy.next = head;
            head = nextNode;
        }
        return dummy.next;
//        Stack<ListNode> s = new Stack<>();
//        while (head != null){
//            ListNode t = head.next;
//            s.push(head);
//            head = t;
//        }
//        ListNode dummy = new ListNode(-1);
//        head = dummy;
//        while (!s.isEmpty()){
//            ListNode t = s.pop();
//            head.next = t;
//            head = t;
//        }
//        return dummy.next;
    }

}

class ListNode {
    int val;
    ListNode next ;

    public ListNode(int val) {
        this.val = val;
    }
}
